﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QcRiverFlows.Models;

namespace QcRiverFlows.Controllers
{
    public class SectionsController : Controller
    {
        private RiverFlowsContext db = new RiverFlowsContext();

        //
        // GET: /Sections/

        public ActionResult Index()
        {
            var sections = db.Sections.Include(s => s.River).Include(s => s.Category);
            return View(sections.ToList());
        }

        //
        // GET: /Sections/Details/5

        public ActionResult Details(int id = 0)
        {
            Section section = db.Sections.Find(id);
            if (section == null)
            {
                return HttpNotFound();
            }
            return View(section);
        }

        //
        // GET: /Sections/Create

        public ActionResult Create()
        {
            ViewBag.RiverId = new SelectList(db.Rivers, "Id", "Name");
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name");
            return View();
        }

        //
        // POST: /Sections/Create

        [HttpPost]
        public ActionResult Create(Section section)
        {
            if (ModelState.IsValid)
            {
                db.Sections.Add(section);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.RiverId = new SelectList(db.Rivers, "Id", "Name", section.RiverId);
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", section.CategoryId);
            return View(section);
        }

        //
        // GET: /Sections/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Section section = db.Sections.Find(id);
            if (section == null)
            {
                return HttpNotFound();
            }
            ViewBag.RiverId = new SelectList(db.Rivers, "Id", "Name", section.RiverId);
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", section.CategoryId);
            return View(section);
        }

        //
        // POST: /Sections/Edit/5

        [HttpPost]
        public ActionResult Edit(Section section)
        {
            if (ModelState.IsValid)
            {
                db.Entry(section).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.RiverId = new SelectList(db.Rivers, "Id", "Name", section.RiverId);
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", section.CategoryId);
            return View(section);
        }

        //
        // GET: /Sections/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Section section = db.Sections.Find(id);
            if (section == null)
            {
                return HttpNotFound();
            }
            return View(section);
        }

        //
        // POST: /Sections/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Section section = db.Sections.Find(id);
            db.Sections.Remove(section);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}